package rs.mbs2.epics.server.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@SuppressWarnings("serial")
@Entity
@Table(name="album")
@NamedQueries({
	@NamedQuery(name="findAlbumByTitleAndUserId", 
				query="SELECT a FROM Album a WHERE a.title " +
					"LIKE :title AND a.user.id LIKE :userId"),
	
	@NamedQuery(name="findAlbumsFromUser", 
				query="SELECT a FROM Album a WHERE a.user.id "
						+ "LIKE :userId"),
	
	@NamedQuery(name="findAlbumsByPhotoId",
				query="SELECT a FROM Album a LEFT JOIN a.photos p WHERE p.id LIKE :photoId"),
				
	@NamedQuery(name="findAlbumById",
				query="SELECT a FROM Album a WHERE a.id LIKE :albumId")
})
public class Album implements Serializable {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "album_id", unique = true, nullable = false)
	private Integer id;
	
	@Column(name = "album_title", unique = false, nullable = false)
	private String  title;
	
	@Column(name = "album_description", unique = false, nullable = true)
	private String  desription;
	
	@Column(name = "album_public_available", nullable = false)
	private Boolean publiclyAvailable;
	
	@ManyToOne
	@JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false)
	private User user;
	
	@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
	@JoinTable( name = "photos_and_albums",
			joinColumns = { @JoinColumn(name = "album_id") },
			inverseJoinColumns = { @JoinColumn(name = "photo_id", referencedColumnName = "photo_id") } )
	private Set<Photo> photos;
	
	
	public Album() {
		
	}
	
	public Album(String title, String desription, Boolean publiclyAvailable, User user) {
		
		super();
		this.title = title;
		this.desription = desription;
		this.publiclyAvailable = publiclyAvailable;
		this.user = user;
		this.photos = new HashSet<>();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Album other = (Album) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDesription() {
		return desription;
	}

	public void setDesription(String desription) {
		this.desription = desription;
	}

	public Boolean getPubliclyAvailable() {
		return publiclyAvailable;
	}

	public void setPubliclyAvailable(Boolean publiclyAvailable) {
		this.publiclyAvailable = publiclyAvailable;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Set<Photo> getPhotos() {
		return photos;
	}

	public void setPhotos(Set<Photo> photos) {
		this.photos = photos;
	}

}
